package com.mll.snake.component;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.MaskFilter;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.TouchEvent;


/**
 * 自定义游戏摇杆
 */
public class GameRockerComponent extends Component {
    private Paint outerCirclePaint;
    private Paint innerCirclePaint;
    /** 内圆中心x坐标 */
    private double innerCenterX;
    /** 内圆中心y坐标 */
    private double innerCenterY;

    public float getViewCenterX() {
        return viewCenterX;
    }

    public void setViewCenterX(float viewCenterX) {
        this.viewCenterX = viewCenterX;
    }

    public float getViewCenterY() {
        return viewCenterY;
    }

    public void setViewCenterY(float viewCenterY) {
        this.viewCenterY = viewCenterY;
    }

    /** view中心点x坐标 */
    private float viewCenterX;
    /** view中心点y左边 */
    private float viewCenterY;
    /** view宽高大小，设定宽高相等 */
    private int size;
    /** 外圆半径 */
    private int outerCircleRadius;
    /** 内圆半径 */
    private int innerCircleRadius;

    private DrawTask task = new DrawTask() {
        @Override
        public void onDraw(Component component, Canvas canvas) {
            canvas.drawCircle(viewCenterX, viewCenterY, outerCircleRadius, outerCirclePaint);
            canvas.drawCircle((float) innerCenterX, (float) innerCenterY, innerCircleRadius, innerCirclePaint);
        }

    };
    public GameRockerComponent(Context context) {
        super(context);
        init();
        addDrawTask(task);
        this.setTouchEventListener(new TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                if (touchEvent.getAction() == TouchEvent.PRIMARY_POINT_UP) {
                    restorePosition();
                } else {
                    handleEvent(touchEvent);
                }

                return true;
            }
        });
    }

//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//
//
//    }

    private void init() {
        size = 200;

        innerCenterX = size/2;
        innerCenterY = size/2;
        viewCenterX = size/2;
        viewCenterY = size/2;
        outerCircleRadius = size/2;
        innerCircleRadius = size/5;
        outerCirclePaint = new Paint();
        outerCirclePaint.setColor(Color.GREEN);
        outerCirclePaint.setAlpha(0.6f);
        outerCirclePaint.setAntiAlias(true);
        outerCirclePaint.setMaskFilter(new MaskFilter(50, MaskFilter.Blur.INNER));

        innerCirclePaint = new Paint();
        innerCirclePaint.setAlpha(0.7f);
        innerCirclePaint.setColor(Color.RED);
        innerCirclePaint.setAntiAlias(true);
    }

    public GameRockerComponent(Context context, AttrSet attrSet, Paint outerCirclePaint) {
        super(context, attrSet);
        init();
    }

    /**
     * 处理手势事件
     */
    public void handleEvent(TouchEvent event) {
        MmiPoint point = event.getPointerScreenPosition(0);
        double distance = Math.sqrt(Math.pow(point.getX()-viewCenterX, 2) + Math.pow(point.getY()-viewCenterY, 2)); //触摸点与view中心距离
        int n = outerCircleRadius-innerCircleRadius;
        System.out.println("++++++++++++_______________________+++++++++++++++" + distance + "+++++++++" + n);
        if (distance < outerCircleRadius-innerCircleRadius) {
            //在自由域之内，触摸点实时作为内圆圆心
            innerCenterX = point.getX();
            innerCenterY = point.getY();
            invalidate();
        } else {
            //在自由域之外，内圆圆心在触摸点与外圆圆心的线段上
            updateInnerCircelCenter(event);
        }
    }

    /**
     * 在自由域外更新内圆中心坐标
     */
    public void updateInnerCircelCenter(TouchEvent event) {
        MmiPoint point = event.getPointerScreenPosition(0);
        double distance = Math.sqrt(Math.pow(point.getX()-viewCenterX, 2) + Math.pow(point.getY()-viewCenterY, 2));  //当前触摸点到圆心的距离
        int innerDistance = outerCircleRadius-innerCircleRadius;  //内圆圆心到中心点距离
        //相似三角形的性质，两个相似三角形各边比例相等得到等式
        innerCenterX = (point.getX()-viewCenterX)*innerDistance/distance + viewCenterX;
        innerCenterY = (point.getY()-viewCenterY)*innerDistance/distance + viewCenterY;

        invalidate();
    }

    /**
     * 恢复内圆到view中心位置
     */
    public void restorePosition() {
        innerCenterX = viewCenterX;
        innerCenterY = viewCenterY;
        invalidate();
    }

}
